<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :edit="edit" :checkedit="checkedit" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {
		buildSubFormData,
		subOnLoad,
		changeEditor,
		configSelectData
	} from '../../../static/js/formUtil.js';
	import {
		ignoreHtml,
		ptFloat,
		minusF,
		getDateStr,
		errorModal
	} from '@/static/js/common.js';
	import urlUtil from '@/static/js/urlUtil.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				newProductIdCacheKey:'pur-qg-requisions-title2-newProduct-cache',
				bomShowCache:'pur-qg-requisions-title2-bomShow-cache',
				instCache:'pur-qg-requisions-title2-inst-cache',
				sluggishResponsiblePartiesCache:'pur-qg-requisions-title2-sluggishResponsibleParties-cache',
				purchaseUserCache:'pur-qg-requisions-title2-purchaseUser-cache',
				siteZoningCache:'pur-qg-requisions-title2-siteZoning-cache',
				warehousingUserCache:'pur-qg-requisions-title2-warehousingUser-cache',
				arrivedBatchesMax:0, // 批次
				arrivedBatchesMin:0,
				arrivalIntervalDaysMax:0, //天数
				arrivalIntervalDaysMin:0,
				arrivedBatches:'',
				arrivalIntervalDays:'',
				requirementTime:'',
				instId:'',
				inst:'',
				sluggishInstId:'',
				sluggishInstName:'',
				instId:'',
				inst:'',
				sluggishInstId:'',
				sluggishInstName:'',
				materialsId:4901,
				materialsStr:"√"
			})
		},
		methods:{
			verifyMaterialsStr(id){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				let supplierData = null
				_that.om.request(
					'purchaseOrder/verifyMaterialsStr',
					{
						newProductId:id
					}
				).then(res=>{
					if(res.succeed && res.value && res.value.newProductId) {
					    supplierData = res.value;
					}
					formdata.materialsId = !supplierData ? _that.materialsId : ''
					formdata.materialsStr = !supplierData ? _that.materialsStr : ''
					_that.getAccordingProductHaveBom(id)
				})
			},
			getSupplierByProductId(id,signCode){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				let supplierData = null
				_that.om.request(
					'purchaseOrder/getSupplierByProductId',
					{
						newProductId: id,
						productSignCode: signCode
					}
				).then(res=>{
					if(res.succeed && res.value && res.value.newProductId) {
					    supplierData = res.value;
					}
					if (supplierData ) {
						if (supplierData.pricingFlag) {
							formdata.undeterminedPriceId = _that.materialsId
							formdata.undeterminedPriceName = _that.materialsStr
							formdata.unitPriceIncludingTax = ''
							formdata.supplierId = ''
							formdata.supplierName = ''
							formdata.taxRate = ''
						} else {
							formdata.unitPriceIncludingTax = supplierData.unitPriceIncludingTax
							formdata.supplierId = supplierData.supplierId
							formdata.supplierCode = supplierData.supplierCode
							formdata.supplierName = supplierData.supplierName
							formdata.taxRate = supplierData.taxRate
							formdata.undeterminedPriceId =''
						}
					}
				})
			},
			getDataBySiteId(siteId){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				_that.om.request(
					'warehousingConfiguration/getDataBySiteId',
					{
						siteId: siteId
					}
				).then(res=>{
					formdata.warehousingUserId = res.value.warehousingUserId
					formdata.warehousingUserName = res.value.warehousingUserName
					formdata.siteZoningId = res.value.siteZoningId
					formdata.siteZoningName = res.value.siteZoningName
					formdata.siteZoningCode = res.value.siteZoningCode
				})
			},
			clearPurchaseUser(){
				let _that = this
				_that.$refs.autoform.clear("title2","purchaseUserId")
				_that.$refs.autoform.clear("title2","purchaseZinstId")
				_that.$refs.autoform.changeEditor("title2","purchaseZinstId",'data',configSelectData([],'id','instName'))
			},
			findInstByUserIdOrPostId(id,filedId){
				let _that = this
				_that.om.request(
					'orgInstPosUserOfficial/findInstByUserIdOrPostId',
					{
						userId:id
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("title2",filedId,'data',configSelectData(res,'id','instName'))
					_that.$refs.autoform.initSelectIndex("title2",filedId)
				})
			},
			clearNewProductId(){
				let _that = this
				_that.$refs.autoform.clearGroup("title2")
			},
			onInputArrivedBatches(val){//输入次数
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				let num = Number(val)
				if(_that.arrivedBatchesMin+'' && num <_that.arrivedBatchesMin){
					formdata.arrivedBatches = _that.arrivedBatchesMin
				}
				if(_that.arrivedBatchesMax+'' && num >_that.arrivedBatchesMax){
					formdata.arrivedBatches = _that.arrivedBatchesMax
				}
			},
			onInutArrivalIntervalDays(val){ // 输入天数
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				let num = Number(val)
				if(!(val + '')){
					return
				}
				if(formdata.arrivedBatches && formdata.arrivedBatches == 1){
					let title = _that.$refs.autoform.getEditorTitle("title2","arrivedBatches")
					let title1 = _that.$refs.autoform.getEditorTitle("title2","arrivalIntervalDays")
					_that.om.toast(title+'为1时，'+title1+'不能存在数据')
					_that.$refs.autoform.clear("title2","arrivalIntervalDays")
					return
				}
				if(_that.arrivalIntervalDaysMin+'' && num <_that.arrivalIntervalDaysMin){
					formdata.arrivalIntervalDays = _that.arrivalIntervalDaysMin
				}
				if(_that.arrivalIntervalDaysMax+'' && num >_that.arrivalIntervalDaysMax){
					formdata.arrivalIntervalDays = _that.arrivalIntervalDaysMax
				}
			},
			onInputRequirementTime(val){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				if(formdata.purchaseTime){
					if(moment(val).isBefore(formdata.purchaseTime)){
						let title = _that.$refs.autoform.getEditorTitle("title2","purchaseTime")
						let title1 = _that.$refs.autoform.getEditorTitle("title2","requirementTime")
						errorModal(_that,'提示信息',title1+'间不能早于'+title+formdata.purchaseTime,()=>{
							_that.$refs.autoform.clear("title2","requirementTime")
						})
					}else{
						formdata.lastRequirementTime = val
					}
				}
			},
			verifyBomShowVersion(){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				
				if(!formdata.newProductId){
					let title1 = _that.$refs.autoform.getEditorTitle("title2","newProductId")
					_that.om.toast(title1+'需优先完成编辑！')
					return false
				}else{
					if(!formdata.bomShowId){
						let title1 = _that.$refs.autoform.getEditorTitle("title2","bomShowId")
						errorModal(_that,'提示信息',title1+'为: “'+ formdata.newProductCode+'”的产品没有BOM版本号',()=>{
							
						})
						return false
					}
					_that.$refs.autoform.changeEditor("title2","bomShowId",{
						link:'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify({
								productShowId: formdata.newProductId,
								firstId: formdata.bomShowId
							}),
							url:'bomShow/getBomShowByProductShowPage',
							namefield:'identificationCode',
							notefield:'versionDesc',
							initload:true,
							cachename:_that.bomShowCache
						}
					})
					return true
				}
			},
			getAccordingProductHaveBom(id){
				let _that = this
				_that.om.request(
					'bomShow/getAccordingProductHaveBom',
					{
						productShowId: id
					}
				).then(res=>{
					if(res.succeed){
						_that.$refs.autoform.formdata.productSignCode = res.value.newProductCode
						if(res.value.bomShowResponse){
							_that.$refs.autoform.formdata.bomShowId = res.value.bomShowResponse.bomShowId
							_that.$refs.autoform.formdata.bomShowVersionNumber = res.value.bomShowResponse.identificationCode
						}
						_that.getSupplierByProductId(id,res.value.newProductCode)
					}
				})
			},
			initload(){
				let _that = this
			},
		},
		onLoad(e) {
			let _that = this
			let arr = [
				{
					url:'dictSource/loadCombo',
					param:{
						sourceId:'283',
						categoryId:'4900'
					}
				}	
			]
			subOnLoad(_that, e, function(ajaxData) {
				if (e.configDataCacheKey) {
					if(e.arrivedBatchesMax){
						_that.arrivedBatchesMax = Number(e.arrivedBatchesMax)
					}
					if(e.arrivedBatchesMin){
						_that.arrivedBatchesMin = Number(e.arrivedBatchesMin)
					}
					if(e.arrivalIntervalDaysMax){
						_that.arrivalIntervalDaysMax = Number(e.arrivalIntervalDaysMax)
					}
					if(e.arrivalIntervalDaysMin){
						_that.arrivalIntervalDaysMin = Number(e.arrivalIntervalDaysMin)
					}
					if(e.requirementTime){
						_that.requirementTime = e.requirementTime
					}
					if(e.arrivedBatches){
						_that.arrivedBatches = Number(e.arrivedBatches)
					}
					if(e.arrivalIntervalDays){
						_that.arrivalIntervalDays = Number(e.arrivalIntervalDays)
					}
					if(e.instId){
						_that.instId = e.instId
					}
					if(e.inst){
						_that.inst = e.inst
					}
					if(e.sluggishInstId){
						_that.sluggishInstId = e.sluggishInstId
					}
					if(e.sluggishInstName){
						_that.sluggishInstName = e.sluggishInstName
					}
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if (res.data && res.data instanceof Array) {
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
							}
							Object.assign(_that.initValue, _that.formdata)
							_that.autoformdata[0].order = _that.order
							changeEditor("title2","newProductId",{
								link:'../../public/pageSearch/pageSearch',
								"linkParam": {
									url:'productShowSecond/getProductPage',
									namefield: 'newProductCode',
									extrafield:'newProductStd',
									notefield:'newProductName',
									searchfield: "q",
									initload:true,
									cachename: _that.newProductIdCacheKey,
								}
							},null,_that.autoformdata)
							changeEditor("title2","sampleId","data",configSelectData(ajaxData[0],'id','text'),_that.autoformdata)
							
							changeEditor("title2","instId",{
								"link":'../../public/search/search',
								"linkParam":{
									other:JSON.stringify(),
									url:'tinstitution/getComboTree',
									namefield:'text',
									initload:true,
									cachename:_that.instCache
								}
							},null,_that.autoformdata)
							
							changeEditor("title2","sluggishResponsiblePartiesId",{
								"link":'../../public/search/search',
								"linkParam":{
									other:JSON.stringify(),
									url:'tinstitution/getComboTree',
									namefield:'text',
									initload:true,
									cachename:_that.sluggishResponsiblePartiesCache
								}
							},null,_that.autoformdata)
							changeEditor("title2","siteZoningId",{
								"link":'../../public/pageSearch/pageSearch',
								"linkParam":{
									other:JSON.stringify(),
									url:'warehouseArchives/getStorageWarehouseArchivesPager',
									namefield:'typeName',
									initload:true,
									cachename:_that.siteZoningCache
								}
							},null,_that.autoformdata)
							
							changeEditor("title2","purchaseUserId",{
								"link":'../../public/searchUser/searchUser',
								"linkParam":{
									other:JSON.stringify({
									}),
									isSingle:true,
									isUserPos:true,
									isNew:true,
									cachename:_that.purchaseUserCache
								}
							},null,_that.autoformdata) 
							
							changeEditor("title2","warehousingUserId",{
								"link":'../../public/pageSearch/pageSearch',
								"linkParam":{
									other:JSON.stringify({
										firstUserId:_that.formdata.warehousingUserId?_that.formdata.warehousingUserId:''
									}),
									url:'temployee/getStaffCombogrid',
									namefield:'userPosInstName',
									searchfield:'q',
									initload:true,
									cachename:_that.warehousingUserCache
								}
							},null,_that.autoformdata)
							
							changeEditor("title2","requirementTime",'noInitDate',true,_that.autoformdata)
							_that.$nextTick(function(){
								if(_that.formdata.purchaseUserId){
									_that.findInstByUserIdOrPostId(_that.formdata.purchaseUserId,'purchaseZinstId')
								}
								_that.$refs.autoform.initSelectIndex("title2","sampleId")
							})
						}
					})
				}
			},arr)
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				uni.getStorage({
					key:_that.newProductIdCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.newProductCode = d.newProductCode
							formdata.newProductId = d.id
							formdata.bomShowId = d.bomShowId
							formdata.bomShowVersionNumber =  d.bomShowVersionNumber
							formdata.productSignCode =  d.productSignCode
							formdata.productCode =  d.productCode
							formdata.newProductName =  d.newProductName
							formdata.newProductStd =  d.newProductStd
							formdata.englishName =  d.englishName
							formdata.commonName =  d.commonName
							formdata.projectNeed =  d.projectNeed
							formdata.projectFeature =  d.projectFeature
							formdata.unitId =  d.unitId
							formdata.unitName =  d.unitName
							formdata.stockQuantity =  d.stockQuantity
							formdata.availableQuantity =  d.availableQuantity
							formdata.inventoryOutlook =  d.inventoryOutlook
							formdata.arrivedBatches = _that.arrivedBatches
							formdata.arrivalIntervalDays= _that.arrivalIntervalDays
							formdata.requirementTime= _that.requirementTime
							formdata.lastRequirementTime = _that.requirementTime
							formdata.instId = _that.instId
							formdata.inst = _that.inst
							formdata.sluggishResponsiblePartiesId = _that.sluggishInstId
							formdata.sluggishResponsibleParties = _that.sluggishInstName
							_that.getDataBySiteId(d.defaultRepository)
							_that.verifyMaterialsStr(d.id)
							formdata.standardParts = d.standardParts
							formdata.standardPartsStr = d.standardPartsStr
							uni.removeStorage({
								key:_that.newProductIdCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.bomShowCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.bomShowId = d.bomShowId
							formdata.bomShowVersionNumber = d.identificationCode
							uni.removeStorage({
								key:_that.bomShowCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.instCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.instId = d.id
							formdata.inst = d.text
							uni.removeStorage({
								key:_that.instCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.sluggishResponsiblePartiesCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.sluggishResponsiblePartiesId = d.id
							formdata.sluggishResponsibleParties = d.text
							uni.removeStorage({
								key:_that.sluggishResponsiblePartiesCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.siteZoningCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.siteZoningId = d.id
							formdata.siteZoningName = d.typeName
							formdata.siteZoningCode = d.typeCode
							uni.removeStorage({
								key:_that.siteZoningCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.purchaseUserCache,
					success:(res)=>{
						if(res.data){
							let d = res.data[0]
							let formdata = _that.$refs.autoform.formdata
							formdata.purchaseUserId = d.id
							formdata.purchaseUserName = d.username
							formdata.purchaseZinstId = d.instId
							formdata.purchaseZinstName = d.instName
							_that.findInstByUserIdOrPostId(d.id,'purchaseZinstId')
							uni.removeStorage({
								key:_that.purchaseUserCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.warehousingUserCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.warehousingUserId = d.id
							formdata.warehousingUserName = d.username
							uni.removeStorage({
								key:_that.warehousingUserCache
							})
						}
					}
				})
				
			}
		}
	}
</script>

<style>
</style>
